#include "PrecompiledHeaders.h"
#include "Mouse.h"

/*
 * Initialise mouse specific values
 */
Mouse::Mouse(OIS::Mouse *m, GameResources *r) : UserControlledInputDevice()
{
    mouse = m;
    max_x = r->config->getResolutionWidth() / 2;
    max_y = r->config->getResolutionHeight() / 2;
    x_factor = Constants::MAX_ANGLE.valueRadians() / max_x;
    y_factor = Constants::MAX_ANGLE.valueRadians() / max_y;
}

Ogre::Radian Mouse::getX()
{
    previous_x += x_rel;
    x_rel = 0;
    if(previous_x >  max_x) previous_x =  max_x;
    if(previous_x < -max_x) previous_x = -max_x;
    return Ogre::Radian(previous_x);
}
 
Ogre::Radian Mouse::getY()
{
    previous_y += y_rel;
    y_rel = 0;
    if(previous_y >  max_y) previous_y =  max_y;
    if(previous_y < -max_y) previous_y = -max_y;
    return Ogre::Radian(previous_y);
}

void Mouse::initialise()
{
    previous_x = previous_y = x_rel = y_rel = 0;
    pitch = roll = Ogre::Radian(0);
}

void Mouse::setWidth(unsigned int width)
{
    mouse->getMouseState().width = width;
    max_x = width / 2;
    x_factor = Constants::MAX_ANGLE.valueRadians() / max_x;
}

void Mouse::setHeight(unsigned int height)
{
    mouse->getMouseState().height = height;
    max_y = height / 2;
    y_factor = Constants::MAX_ANGLE.valueRadians() / max_y;
}

void Mouse::capture()
{
     mouse->capture();
     x_rel += mouse->getMouseState().X.rel;
     y_rel += mouse->getMouseState().Y.rel;
}
